import { createRouter, createWebHashHistory } from 'vue-router'
import { ElMessage } from 'element-plus'
import { getToken } from '@/utils/storage'
const router = createRouter({
  // 使用hash模式
  history: createWebHashHistory(),
  routes: [
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/Login/index.vue'),
      meta: {
        title: '登录',
        requiresAuth: false
      }
    },
    {
      path: '/',
      component: () => import('@/views/Layout/index.vue'),
      redirect: '/home',
      meta: {
        title: '首页',
        requiresAuth: true
      },
      children: [
        {
          path: 'home',
          component: () => import('@/views/Home/index.vue'),
          meta: { title: '首页', requiresAuth: true }
        },
        {
          path: 'students',
          component: () => import('@/views/Students/index.vue'),
          meta: { title: '学生管理', requiresAuth: true }
        },
        {
          path: 'clazzDetail',
          component: () => import('@/views/Detail/clazz.vue'),
          meta: { title: '班级详情', requiresAuth: true }
        },
        {
          path: 'studentsDetail',
          component: () => import('@/views/Detail/grade.vue'),
          meta: { title: '学生详情', requiresAuth: true }
        },
        {
          path: 'courses',
          component: () => import('@/views/Courses/index.vue'),
          meta: { title: '课程管理', requiresAuth: true }
        },
        {
          path: 'clazz_course',
          component: () => import('@/views/Detail/clazz_course.vue'),
          meta: { title: '班级课程详情', requiresAuth: true }
        },
        {
          path: 'score',
          component: () => import('@/views/Detail/score.vue'),
          meta: { title: '班级课程个人成绩', requiresAuth: true }
        },
        {
          path: 'departments',
          component: () => import('@/views/Departments/index.vue'),
          meta: { title: '系部管理', requiresAuth: true }
        },
        {
          path: 'teacher',
          component: () => import('@/views/Detail/teacher.vue'),
          meta: { title: '教师详情', requiresAuth: true }
        },
        {
          path: 'about',
          component: () => import('@/views/About/index.vue'),
          meta: { title: '关于', requiresAuth: true }
        },
        {
          path: 'test',
          component: () => import('@/views/test/index.vue'),
          meta: { title: '测试', requiresAuth: true }
        }
      ]
    },
    {
      path: '/404',
      component: () => import('@/views/NotFound/index.vue'),
      meta: {
        title: '未找到',
        requiresAuth: false
      }
    },
    // 任意未匹配到的路径 都重定向到404
    {
      path: '/:pathMatch(.*)*',
      redirect: '/404'
    }
  ]
})
// // 路由前置守卫
// 辅助函数（暂时注释掉，后续需要时启用）
const checkAuth = (to, token) => {
  const needAuth = to.matched.some(r => r.meta?.requiresAuth)
  const isLoginPage = to.path === '/login'
  // 未登录且需要登录的情况
  if (needAuth && !token) {
    ElMessage.error('请先登录')
    return '/login'
  }
  // 已登录且访问登录页的情况
  if (token && isLoginPage) {
    ElMessage.warning('您已登录，请勿重复登录')
    return '/'
  }

  return null // 放行
}

// 前置守卫
router.beforeEach((to, from, next) => {
  if (to.meta?.title) {
    document.title = to.meta.title
  }
  // 当后端 token写好后开启
  const token = getToken()
  const redirect = checkAuth(to, token)

  next(redirect || undefined)
})
export default router
